# *****************************************************************
#
#  Copyright 2015 Samsung Electronics All Rights Reserved.
#
#
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#
# *****************************************************************/
##
# Tinydtls build script
##

Import('env')

print "Reading Tinydtls folder script"

target_os = env.get('TARGET_OS')

if(target_os) == 'arduino':
	env.Replace(CFLAGS = env.get('CXXFLAGS'))

root_dir = './'
tinydtls_src_path = root_dir

env.AppendUnique(CPPPATH = [root_dir])
env.AppendUnique(CPPPATH = [root_dir+'aes/'])
env.AppendUnique(CPPPATH = [root_dir+'ecc/'])
env.AppendUnique(CPPPATH = [root_dir+'sha2/'])

if target_os not in ['arduino', 'windows', 'winrt']:
	env.AppendUnique(CPPDEFINES = ['HAVE_SYS_TIME_H'])

######################################################################
# Source files and Target(s)
######################################################################
tinydtls_src = [
                'dtls.c',
                'crypto.c',
                'ccm.c',
                'hmac.c',
                'netq.c',
                'peer.c',
                'dtls_time.c',
                'session.c',
                'aes/rijndael.c',
                'ecc/ecc.c',
                'sha2/sha2.c',
        ]

env.AppendUnique(TINYDTLS_SRC = tinydtls_src)

if not env.get('RELEASE'):
	if(target_os) not in ['arduino']:
		env.AppendUnique(TINYDTLS_SRC = ['debug.c'])
	else:
		env.AppendUnique(CPPDEFINES = ['NDEBUG'])
else:
	env.AppendUnique(CPPDEFINES = ['NDEBUG'])

env.AppendUnique(CPPDEFINES = ['DTLSV12',  'WITH_SHA256', 'DTLS_CHECK_CONTENTTYPE'])


libtinydtls = env.StaticLibrary('libtinydtls', env.get('TINYDTLS_SRC'), OBJPREFIX='libtinydtls_')

######################################################################
# Generate tinydtls samples
#
# Note:
# Currently there is a bug in debug.h/debug.c which fails compilation
# of tinydtls samples in release mode. This bug is being tracked in
# IOT-395
######################################################################
if not env.get('RELEASE'):
	samples_env = env.Clone()

	if target_os not in ['arduino', 'windows', 'winrt']:
		samples_env.AppendUnique(CPPDEFINES = ['_GNU_SOURCE'])

	dtlsserver = samples_env.Program('dtls-server', ['tests/dtls-server.c'])
	dtlsclient = samples_env.Program('dtls-client', ['tests/dtls-client.c'])

	samples_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
	samples_env.PrependUnique(LIBS = ['tinydtls'])

	Alias("samples", [dtlsserver, dtlsclient])

	samples_env.AppendTarget('samples')

env.InstallTarget(libtinydtls, 'libtinydtls');
env.UserInstallTargetLib(libtinydtls, 'libtinydtls');

